package com.example.take_out_admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.take_out_admin.common.Result;
import com.example.take_out_admin.config.JwtConfig;
import com.example.take_out_admin.entity.Employee;
import com.example.take_out_admin.service.EmployeeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author yyyyys
 * @since 2024-06-29
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    @Resource
    private HttpServletRequest request;

    @PostMapping(value = "login")
    public Result<String> login(@RequestBody Employee employee){
        String token = employeeService.login(employee);
        return Result.success(token);
    }

    @GetMapping(value = "{id}")
    public Result<Employee> findById(@PathVariable long id){
        Employee employee = employeeService.getById(id);
        employee.setPassword(null);
        return Result.success(employee);
    }
    @GetMapping(value = "page")
    public Result<Page<Employee>> page(int page, int pageSize, String name){
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(name),Employee::getName,name);
        Page<Employee> pageInfo = employeeService.page(new Page<>(page, pageSize), wrapper);
        return Result.success(pageInfo);
    }

    @PostMapping()
    public Result<Object> save(@RequestBody Employee employee){
        employeeService.saveEmployee(employee);
        return Result.success();
    }
    @PostMapping("/status")
    public Result<Object> updateStatus(@RequestParam("id") String id){
        Employee employee = employeeService.getById(id);
        if (employee.getStatus()==0){
            employee.setStatus(1);
        }else {
            employee.setStatus(0);
        }
        employeeService.updateById(employee);
        return Result.success();
    }

    @PutMapping()
    public Result<Object> update(@RequestBody Employee employee){
        this.employeeService.updateEmployee(employee);
        return Result.success();
    }
    @PutMapping("editPassword")
    public Result<Object> editPassword( String oldPassword,String newPassword){
        Employee employee = (Employee) request.getSession().getAttribute("employee");
        this.employeeService.updateEmployee(employee);
        return Result.success();
    }


}
